<template>
  <div class="mgtMobile">
    <x-header v-show="!isWeChar">
      <div slot="default" class="common__header--title">{{pageTitle}}</div>
      <div @click="goBack" slot="overwrite-left"><img slot="icon" src="~assets/x3/ico/goback1.png" class="go-back"></div>
    </x-header>
    <div class="mgtMobile_inputDiv">
      <x-input type="tel" mask="99999999999" :max="StrLenth.mobile" v-model="mobile" placeholder="请输入新手机号码" :disabled="isSendOk">
        <img slot="label" style="padding-right:10px;display:block;" src="~assets/x3/ico/ico-input-linkMobile.png" width="18">
      </x-input>
      <x-input v-if="haveImgCode" v-model="imgCode" :max="StrLenth.img" placeholder="请输入图形验证码">
        <img slot="label" style="padding-right:10px;padding-top:5px;display:block;" src="~assets/x3/ico/ico-input-img_code.png" width="18">
        <span slot="right" class="img__code--change" @click="changeImgCode()">换一张</span>
        <img slot="right" class="img__code" :src="imgCodeSrc" @click="changeImgCode()">
      </x-input>
      <x-input type="tel" :max="StrLenth.sms" v-model="mobileCode" placeholder="请输入短信验证码">
        <img slot="label" style="padding-right:10px;padding-top:5px;display:block;" src="~assets/x3/ico/ico-input-Mcode.png" width="18">
        <x-button action-type="button" slot="right" mini class="linkMobileCode" @click.native="sendSmsCode" :text="sendBtnText" :disabled="isSendOk"></x-button>
      </x-input>
    </div>
    <div class="mgtMobile_errorMsg" v-show="haveError">
      <badge text="!"></badge> {{errorMessage}}
    </div>
    <x-button text="保存" @click.native="processForm" type="primary" class="mgtMobile_process_btn"></x-button>
  </div>
</template>
<script>
import { XHeader, XInput, XButton, Badge, Alert, AlertPlugin } from "vux";
import { RouteUrl, Mutations, StrLenth } from "src/common/const";
import Validation from "src/common/util/validation";
import { RequestUrl } from "src/common/url";
import { mapState } from "vuex";

export default {
  components: { XHeader, XInput, XButton, Badge, Alert, AlertPlugin },
  computed: mapState(["user", "isWeChar"]),
  data() {
    return {
      StrLenth: {}, //字符长度常量
      pageTitle: "修改手机号",
      mobile: "", //手机号
      mobileCode: "", //验证码
      isSendOk: false, //发送按钮是否禁用
      sendBtnText: "获取验证码", //发送按钮文本内容
      haveError: false, //是否显示错误
      errorMessage: "", //错误信息

      imgCodeSrc: RequestUrl.GET_VALIDATE_CODE, //验证码图片路径
      imgCode: "", //图形码
      haveImgCode: true //是否需要图形码,false=因为默认为密码登录因此需要,动态码登录第一次不需要
    };
  },
  methods: {
    //返回
    goBack() {
      this.$common.goBackUrl(this, RouteUrl.ACCOUNT);
    },
    changeImgCode() {
      this.imgCodeSrc = RequestUrl.GET_VALIDATE_CODE + "?a=" + Math.random();
    },
    // 发送验证码
    sendSmsCode() {
      /**
        * 发送验证码,返回data.msg=信息,data.state=0表示成功
        * isSendOk,true=禁用(disabled=true)
        * sendBtnText,验证码按钮文本显示内容
        */
      let imgcode = null;
      if (this.haveImgCode) {
        //需要图形码时传递参数
        imgcode = this.imgCode;
        //校验-图形码
        let msg = Validation.isImgCode(imgcode);
        if (msg != null) {
          this.errorMessage = msg;
          this.haveError = true;
          return;
        }
      }
      this.$common.sendSMS(this, this.mobile, imgcode, this.isSendOk, this.pageTitle);
    },
    //提交
    processForm() {
      if (!this.user.isLogin) {
        this.errorMessage = "登录超时";
        this.haveError = true;
        return;
      }
      let initData = {
        TYPE: "FORM",
        userCode: this.user.userDto.userCode,
        mobile: this.mobile,
        mobileCode: this.mobileCode
      };
      //校验-手机号
      let msg = Validation.isPhone11(initData.mobile);
      if (msg != null) {
        this.errorMessage = msg;
        this.haveError = true;
        return;
      }
      let postUrl = RequestUrl.UPDATE_MOBILE;
      if (this.haveImgCode) {
        //带图形验证码接口
        postUrl = RequestUrl.UPDATE_MOBILE_IMG;
        initData.imgCode = this.imgCode;
        //校验-图形码
        msg = Validation.isImgCode(initData.imgCode);
        if (msg != null) {
          this.errorMessage = msg;
          this.haveError = true;
          return;
        }
      }
      //校验-手机验证码
      msg = Validation.isSmsCode(initData.mobileCode);
      if (msg != null) {
        this.errorMessage = msg;
        this.haveError = true;
        return;
      }
      this.haveError = false;
      this.errorMessage = "";
      let _this = this;
      _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, true);
      //提交
      _this.$http.post(postUrl, initData).then(function (res) {
        _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, false);
        if (res.state != 0) {
          if (res.result == "图形码过期" || res.result == "图形码错误") {
            _this.changeImgCode();//更换图形验证码
          }
          if (res.result.indexOf("合并") >= 0) {
            _this.mergeUser(_this, res.result);
            return;
          }
          _this.errorMessage = res.result;
          _this.haveError = true;
          // _this.haveImgCode = true; //第一次未启用时，启用图形验证码
          return;
        }
        //保存USER_INFO信息
        _this.$common.storeCommit(_this, Mutations.SAVE_USER_INFO, res.result);

        let data = {
          _this: _this,
          msgData: {
            headerTitle: "信息提示",
            gaBackUrl: RouteUrl.ACCOUNT,
            buttonUrl: RouteUrl.ACCOUNT,
            title: "修改成功",
            description: "",
            buttonsType: "primary",
            buttonsText: "返回账号设置",
          }
        }
        _this.$common.storeCommit(_this, Mutations.SET_MSG_DATA, data);
      });
    },
    mergeUser(_this, result) {
      _this.$vux.alert.show({
        content: result, //显示内容
        // showInput: false, //是否显示input
        // hideOnBlur: false, //点击遮罩可关闭
        // confirmText: "确认",
        // onCancel() { },
        onShow() { },
        onHide() {
          let data = {
            TYPE: "FORM",
            userCode: _this.user.userDto.userCode,
            mobile: _this.mobile,
          };
          //保存
          _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, true);
          _this.$http.post(RequestUrl.UPDATE_USER_FORMERGE, data).then(function (res) {
            _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, false);
            let msg = res.result;
            if (res.state == 0) {
              msg = "合并账户成功 !";
              //保存USER_INFO信息
              _this.$common.storeCommit(_this, Mutations.SAVE_USER_INFO, res.result);
            }
            let showTime = setTimeout(() => {
              _this.$vux.alert.hide();
              _this.$common.goUrl(_this, RouteUrl.ACCOUNT);
            }, 3000);
            //提示信息
            _this.$vux.alert.show({
              content: _this.$common.alertMsg(msg, (res.state == 0) ? true : "red;"),
              onHide() {
                clearTimeout(showTime);
                _this.$common.goUrl(_this, RouteUrl.ACCOUNT);
              }
            });
          });
        }
      });
    },
    initPage() {
      //清除验证码倒计时
      this.$common.clearTimeOutForSms();
      if (!this.user.isLogin) {
        this.$common.goUrl(this, RouteUrl.LOGIN);
      }
    }
  },
  mounted() {
    document.title = this.pageTitle;
    this.StrLenth = StrLenth; //字符长度常量
    this.$init.subPageInit(this);
    //初始化当前页面信息
    this.initPage();
  }
};
</script>
<style lang="scss" scoped>
//input区域
.mgtMobile_inputDiv {
  margin: 15px auto;
  width: 95%;
  height: auto;
  border-radius: 5px;
  background-color: white;
  .weui-cell {
    border-top: 1px solid #efefef;
  }
  .weui-cell:before {
    border-top: 0px;
  }
}

.mgtMobile_inputDiv-oldName {
  background-color: #dfdfdf;
}

//图形码
.img__code {
  width: 80px;
  height: 31px;
  margin-left: 3px;
  vertical-align: middle;
}

//换一张图形码
.img__code--change {
  margin-left: 3px;
  vertical-align: middle;
  font-size: 14px;
}

//获取验证码按钮
.linkMobileCode {
  padding: 0px;
  width: 80px;
  height: 28px;
  line-height: 28px;
  font-size: 12px;
  color: #C8161D;
  border: 1px solid #C8161D;
  background-color: white;
}

//提交按钮
.mgtMobile_process_btn {
  width: 95%;
  font-size: 20px;
}

//错误信息
.mgtMobile_errorMsg {
  color: red;
  font-size: 14px;
  margin-left: 10px;
  height: 36px;
}

//弹窗信息
.weui-dialog__bd {
  height: 30px;
  line-height: 30px;
}

//修改默认按钮失效时背景颜色
.weui-btn_disabled.weui-btn_primary {
  background-color: #cfcfcf;
}
</style>
